﻿using AutoMapper;
using B.S.BasicData.Api.Read.Application.Command.RBAC;
using B.S.BasicData.Domian.Entities.RBAC;
using B.S.BasicData.ErrorCode;
using B.S.BasicData.Infrastructure.Interface;
using MediatR;
using System.Data;

namespace B.S.BasicData.Api.Read.Application.Handler.RBAC
{
    public class FenpeiHandler : IRequestHandler<FenpeiCommand, ApiResult<int>>
    {
        private readonly IBaseRepository<UserRoleModel> userroleRepo;
        private readonly IMapper mapper;

        public FenpeiHandler(IBaseRepository<UserRoleModel>userroleRepo,IMapper mapper)
        {
            this.userroleRepo = userroleRepo;
            this.mapper = mapper;
        }
        public async Task<ApiResult<int>> Handle(FenpeiCommand request, CancellationToken cancellationToken)
        {
            var res=new ApiResult<int>();
            List<long>userarr=request.Uidlist.Split(',').Select(x=>Convert.ToInt64(x)).ToList();
            foreach (var item in userarr)
            {
                UserRoleModel role = new UserRoleModel()
                {
                    UserId=item,
                    RoleId=request.RoleId,
                };
               await userroleRepo.Add(role);
            }
            res.Message = "添加成功";
           res.Data= userarr.Count;
            res.Code=ApiEnum.Ok;
           return Task.FromResult(res).Result;
        }
    }
}
